const gulp = require('gulp')
const ts = require('gulp-typescript')
const nodemon = require('gulp-nodemon')
const browserSync = require('browser-sync').create()

const tsProject = ts.createProject('tsconfig.json')

gulp.task('copy-cmd', function () {
  return gulp.src('cmd/**/*').pipe(gulp.dest('dist'))
})

gulp.task('tsc', function () {
  // tsProject.src()
  return gulp.src('src/**/*.ts')
    .pipe(tsProject())
    .pipe(gulp.dest('dist'))
})

gulp.task('tsc:w', ['tsc', 'copy-cmd'], function () {
  gulp.watch('src/**/*.ts', ['tsc'])
})

gulp.task('default', ['tsc:w'], function () {
  browserSync.init({
    proxy: 'http://localhost:3000',
    browser: 'chrome',
    port: 7000
  })

  nodemon({
    script: 'dist/app',
    env: {
      'NODE_ENV': 'development'
    }
  }).on('start', function () {
    console.log('app start')
    browserSync.reload()
  })
})
